from django.db import models
# -*- coding: utf-8 -*-
# Create your models here.
from django.db.models import ManyToManyField, DateField


class Information(models.Model):
    CustomerID = models.CharField(max_length=20, null=True, blank=True)
    ComplainedLine = models.CharField(max_length=20, null=True, blank=True)
    ComplainedDriverID = models.CharField(max_length=6, null=True, blank=True)
    Status = models.CharField(max_length=10, null=True, blank=True)
    ComplainedContent = models.CharField(max_length=256, null=True, blank=True)
    ComplainedTime = models.CharField(max_length=20, null=True, blank=True)
    Content = models.CharField(max_length=256, null=True, blank=True)
    ManagerID = models.CharField(max_length=6, null=True, blank=True)
    CarSendTime = models.CharField(max_length=50, null=True, blank=True)
    CarSendDate = models.CharField(max_length=50, null=True, blank=True)
    Car = models.CharField(max_length=50, null=True, blank=True)

    def to_dict(self, fields=('CustomerID', 'ComplainedLine', 'ComplainedDriverID',
                              'Status', 'ComplainedContent', 'ComplainedTime', 'CarSendTime', 'CarSendDate',
                              'Car'), exclude=None):
        return get_dict(self, fields, exclude)

    def to_dict1(self, fields=('CustomerID', 'ComplainedLine', 'ComplainedDriverID',
                               'Status', 'ComplainedContent', 'ComplainedTime', 'Content'), exclude=None):
        return get_dict(self, fields, exclude)

    def to_dict5(self, fields='ComplainedContent', exclude=None):
        return get_dict(self, fields, exclude)

    def to_dict6(self, fields=('CustomerID', 'ComplainedLine', 'ComplainedDriverID',
                               'Status', 'ComplainedContent', 'ComplainedTime', 'Content', 'ManagerID'), exclude=None):
        return get_dict(self, fields, exclude)


def get_dict(self, fields=None, exclude=None):
    data = {}
    for f in self._meta.concrete_fields + self._meta.many_to_many:
        value = f.value_from_object(self)
        if fields and f.name not in fields:
            continue
        if exclude and f.name in exclude:
            continue
        if isinstance(f, ManyToManyField):
            value = [i.id for i in value] if self.pk else None
        if isinstance(f, DateField):
            value = value.strftime('%Y-%m-%d') if value else None
        data[f.name] = value
    return data
